﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Kiiro.Common.Utility;
using System.Collections.Generic;

namespace Kiiro.SiteDefinition.ControlTemplates
{
    public partial class WelcomeMessagePanel : System.Web.UI.UserControl
    {
        #region Control Properties

        private bool showPanel = false;
        public bool ShowPanel
        {
            get { return showPanel; }
            set { showPanel = value; }
        }

        private bool manageUsers = false;
        public bool ManageUsers
        {
            get { return manageUsers; }
            set { manageUsers = value; }
        }

        private string serverRelativeUrl = string.Empty;
        public string ServerRelativeUrl
        {
            get { return serverRelativeUrl; }
            set { serverRelativeUrl = value; }
        }

        private string rootFolder = string.Empty;
        public string RootFolder
        {
            get { return rootFolder; }
            set { rootFolder = value; }
        }

        private bool myWorkPage = false;
        public bool MyWorkPage
        {
            get { return myWorkPage; }
            set { myWorkPage = value; }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        internal void ShowHidePanel(string entityType, List<bool> userPermissions)
        {
            kiiroWelcome.Visible = showPanel;

            switch (entityType)
            {
                case Constants.PagerEntity.Projects:
                    litDisplay.Text = ProjectWelcomeMessage(userPermissions);
                    kiiroWelcome.Visible = true;
                    break;
                case Constants.PagerEntity.Work:
                    litDisplay.Text = WorkWelcomeMessage(userPermissions);
                    kiiroWelcome.Visible = true;
                    break;
                case Constants.PagerEntity.Documents:
                    litDisplay.Text = DocumentWelcomeMessage(userPermissions);
                    kiiroWelcome.Visible = true;
                    break;
                case Constants.PagerEntity.Discussions:
                    litDisplay.Text = DiscussionWelcomeMessage(userPermissions);
                    kiiroWelcome.Visible = true;
                    break;
                case Constants.PagerEntity.CustomItems:
                    litDisplay.Text = CustomListWelcomeMessage(userPermissions);
                    kiiroWelcome.Visible = true;
                    break;
                default:
                    kiiroWelcome.Visible = false;
                    break;
            }
        }

        private string CustomListWelcomeMessage(List<bool> userPermissions)
        {
            string customMessage = string.Empty;

            if (userPermissions[0])
            {
                headerDiv.Visible = false;
                customMessage = @"<div style='font-size:18px;margin-bottom:15px;'>Create the first <a href='NewForm.aspx'>item</a> for this list.</div>";
            }
            return customMessage;
        }

        private string DocumentWelcomeMessage(List<bool> userPermissions)
        {
            string documentMessage = string.Empty;
            string documentUrl = string.Empty;
            string folderUrl = string.Empty;

            if (userPermissions[0] && ( RootFolder == "Documents" || string.IsNullOrEmpty(RootFolder) ) )
            {
                headerDiv.Visible = false;
                documentMessage = @"<div style='font-size:18px;margin-bottom:15px;'>
                            Upload the first <a href='{0}'>document</a> for this project.
                        </div> 
                        <p style=""margin-bottom:15px;"">Or you can create a <a href='{1}'>folder</a> to organize your documents.</p>";

                documentUrl = ServerRelativeUrl + Constants.ProjectFileUrls.DocumentUpload;
            }
            else if (userPermissions[0] && RootFolder != "Documents")
            {
                headerDiv.Visible = false;
                documentMessage = @"<div style='font-size:18px;margin-bottom:15px;'>
                            Upload the first <a href=""{0}"">document</a> to this folder.
                        </div> 
                        <p style=""margin-bottom:15px;"">Or you can create a <a href=""{1}"">sub-folder</a> to organize your documents.</p>";

                documentUrl = ServerRelativeUrl + Constants.ProjectFileUrls.DocumentUpload;
            }

            if (RootFolder != string.Empty)
            {

                documentUrl = ServerRelativeUrl + Constants.ProjectFileUrls.DocumentUpload + "?RootFolder=" + Server.UrlEncode ( RootFolder );
            }
            else
                documentUrl = ServerRelativeUrl + Constants.ProjectFileUrls.DocumentUpload;

            if ( RootFolder != string.Empty )
                folderUrl = ServerRelativeUrl + Constants.ProjectFileUrls.DocumentUpload + "?Type=1&RootFolder=" + Server.UrlEncode ( RootFolder );
            else
                folderUrl = ServerRelativeUrl + Constants.ProjectFileUrls.DocumentUpload + "?Type=1";

            return string.Format(documentMessage, documentUrl, folderUrl);
        }

        private string DiscussionWelcomeMessage(List<bool> userPermissions)
        {
            string discussionMessage = string.Empty;
   
            if (userPermissions[0])
            {
                headerDiv.Visible = false;
                discussionMessage = @"<div style='font-size:18px;margin-bottom:15px;'>Post the first <a href='NewDiscussion.aspx'>discussion</a> to this project.</div>";
            }
            return discussionMessage;
        }

        private string WorkWelcomeMessage(List<bool> userPermissions)
        {
            string workMessage = string.Empty;

            if (userPermissions[0] && !myWorkPage)
            {
                headerDiv.Visible = false;
                workMessage = @"<div style='font-size:18px;margin-bottom:15px;'>Create the first <a href='NewTask.aspx'>task</a> or <a href='NewIssue.aspx'>issue</a> for this project.</div>"; ;
            }
            else if (myWorkPage)
            {
                headerDiv.Visible = false;
                workMessage = @"<div style='font-size:18px;margin-bottom:15px;'>You don't have any tasks or issues assigned to you.</div><p style='margin-bottom:15px;'>You can check existing tasks or issues at <a href='AllWork.aspx'>All Work</a></p>"; ;
            }

            return workMessage;
        }

        private string ProjectWelcomeMessage(List<bool> userPermissions)
        {
            string projectMessage = string.Empty;
            string projectsUrl = string.Empty;
            string usersUrl = string.Empty;

            if (userPermissions[0] && ManageUsers)
            {
                projectMessage = @"<p style='margin-bottom:15px;'>Get started by creating a <a href='{0}' >project</a> and adding <a href='{1}'>users</a>.</p>";

                projectsUrl = ServerRelativeUrl + Constants.PortalFileUrls.ProjectNew;
                usersUrl = ServerRelativeUrl + Constants.PortalFileUrls.UserAll;
            }
            else if (userPermissions[0])
            {
                projectMessage = @"<p style='margin-bottom:15px;'>Get started by creating a <a href='{0}'>project</a>.</p>";

                projectsUrl = ServerRelativeUrl + Constants.PortalFileUrls.ProjectNew;

            }
            return string.Format(projectMessage,projectsUrl,usersUrl);
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/CONTROLTEMPLATES/Kiiro/WelcomeMessagePanel.ascx.cs $
 * 
 * 8     1/14/10 12:46p Camerons
 * 
 **************************************************************************/